package com.qingcheng.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.Page;
import com.qingcheng.dao.TbParaMapper;
import com.qingcheng.dao.TbTemplateMapper;
import com.qingcheng.domain.ParaDto;
import com.qingcheng.pojo.goods.TbPara;
import com.qingcheng.pojo.goods.TbTemplate;
import com.qingcheng.service.TbParaService;
import com.qingcheng.util.PageUtil;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;


/**
*
* @author Simon
* @date 2020/07/27
*/
@Service
public class TbParaServiceImpl implements TbParaService {

    @Resource
    private TbParaMapper tbParaMapper;

    @Resource
    private TbTemplateMapper tbTemplateMapper;

    @Override
    public List<TbPara> queryAll() {
        List<TbPara> tbPara = tbParaMapper.selectAll();
            return tbPara;
    }

    @Override
    public PageUtil<TbPara> findListAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<TbPara> pages = (Page<TbPara>) tbParaMapper.selectAll();
        PageUtil<TbPara> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
    }


    @Override
    public PageUtil<TbPara> pageQueryByAll(Integer pageNum, Integer pageSize, TbPara tbPara) {

        Example example=new Example(TbPara.class);
        Example.Criteria criteria = example.createCriteria();
        if(tbPara != null) {
            if (!StringUtils.isEmpty(tbPara.getTemplateId())) {
                criteria.andEqualTo("templateId", tbPara.getTemplateId());
            }
            if (!StringUtils.isEmpty(tbPara.getName())) {
                criteria.andLike("name", "%" + tbPara.getName() + "%");
            }
            if (!StringUtils.isEmpty(tbPara.getOptions()))
                criteria.andLike("options", "%" + tbPara.getOptions() + "%");
        }
        PageHelper.startPage(pageNum,pageSize);
        Page<TbPara> pages = (Page<TbPara>) tbParaMapper.selectByExample(example);
        PageUtil<TbPara> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
     }


    @Override
    @Transactional
    public boolean saveTbPara(TbPara tbPara) {
        int insert = tbParaMapper.insert(tbPara);
        if(insert>0){
            TbTemplate tbTemplate = tbTemplateMapper.selectByPrimaryKey(tbPara.getTemplateId());
            tbTemplate.setParaNum(tbTemplate.getParaNum()+1);
            tbTemplateMapper.updateByPrimaryKeySelective(tbTemplate);
            return true;
        }
        return false;
    }

    @Override
    public boolean updateTbPara(TbPara tbPara) {
        int i = tbParaMapper.updateByPrimaryKeySelective(tbPara);
        if(i>0){
            return true;
        }
        return false;
    }


    @Override
    @Transactional
    public boolean deleteById(Integer id) {
        TbPara tbPara = tbParaMapper.selectByPrimaryKey(id);
        TbTemplate tbTemplate = tbTemplateMapper.selectByPrimaryKey(tbPara.getTemplateId());
        tbTemplate.setParaNum(tbTemplate.getParaNum()-1);
        tbTemplateMapper.updateByPrimaryKeySelective(tbTemplate);
        int i = tbParaMapper.deleteByPrimaryKey(id);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public TbPara findById(Integer id) {
        TbPara tbPara = tbParaMapper.selectByPrimaryKey(id);
        return tbPara;
    }

    @Override
    public PageUtil<ParaDto> PageQueryList(Integer pageNum, Integer pageSize,TbPara tbPara){
        // PageHelper.startPage(pageNum,pageSize); 这段代码会在sql的结尾加上limit ?,?
        List<ParaDto> paraDtos = tbParaMapper.QueryParaUniteSpecList(pageNum, pageSize, tbPara);
        PageUtil<ParaDto> pageUtil=new PageUtil<>();
        pageUtil.setRows(paraDtos);
        pageUtil.setTotal(tbParaMapper.UniteCount(tbPara));
        return pageUtil;
    }

    @Override
    public List<TbPara> queryParaByTemId(Integer templateId) {
        Example example = new Example(TbPara.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("templateId",templateId);
        return tbParaMapper.selectByExample(example);
    }
}
